#include "widget.h"
#include "ui_widget.h"

#include <QToolBox>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QTextEdit>
#include <QMap>
#include <QString>
#include <QIcon>
#include <QToolButton>
#include <QDialog>
#include <QDebug>


Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setFixedSize(600, 480);
    this->setWindowTitle("QQ");
    this->setWindowIcon(QIcon(":/icons/QQ.png"));


    // 好友列表
    QToolBox* tool = new QToolBox(this);
    tool->resize(180, 450);
    tool->move(10, 5);
    tool->setFrameShape(QFrame::Box);

    QMap<QString, QMap<QString, QIcon>> friendInfo = {
        {"我的好友1", {
             {"小孩哥", QIcon(":/icons/icon-test.png")},
             {"一生平安", QIcon(":/icons/icon-test_1.png")},
             {"万事如意", QIcon(":/icons/icon-test_2.png")},
             {"花开富贵", QIcon(":/icons/touxiang.png")},
             {"发福奥利奥", QIcon(":/icons/qiatongtouxiang.png")},
             {"花开富贵2", QIcon(":/icons/touxiang.png")},
             {"花开富贵3", QIcon(":/icons/touxiang.png")}

         }},
        {"陌生人", {
             {"七月上", QIcon(":/icons/BARISTA.png")},
             {"八月下", QIcon(":/icons/touxiang_1.png")},
             {"九月初", QIcon(":/icons/touxiang_2.png")},
             {"十月底", QIcon(":/icons/touxiang_3.png")},
             {"六月憩", QIcon(":/icons/touxiang.png")},
             {"五月睡", QIcon(":/icons/touxiang.png")},
             {"四月躺", QIcon(":/icons/touxiang.png")}
         }},
        {"我的好友2", {
             {"忧郁小薯条", QIcon(":/icons/user__easyico.png")},
             {"enfp小奶狗", QIcon(":/icons/user__easyico_1.png")},
             {"麻辣酸菜鱼", QIcon(":/icons/icon-test_2.png")},
             {"困成憨包", QIcon(":/icons/wutouxiang.png")},
             {"花开富贵", QIcon(":/icons/touxiang.png")},
             {"明人不放暗屁", QIcon(":/icons/touxiang.png")},
             {"煎 bingo 子", QIcon(":/icons/touxiang.png")},
         }}
    };
    for(auto it = friendInfo.begin(); it != friendInfo.end(); it++)
    {
        QWidget* w = new QWidget;
        QVBoxLayout* v = new QVBoxLayout(w);
        v->setAlignment(Qt::AlignTop);
        for (auto it1 = it.value().begin(); it1 != it.value().end(); it1++)
        {
            QPushButton* btn = new QPushButton(it1.value(), it1.key());
            connect(btn, &QPushButton::clicked, [it1](){
                QDialog* dia = new QDialog;
                dia->resize(600, 400);
                dia->setWindowTitle(it1.key());
                dia->setWindowIcon(it1.value());
                dia->setAttribute(Qt::WA_DeleteOnClose);

                QTextEdit* edit = new QTextEdit(dia);
                edit->resize(600, 250);
                edit->setFont(QFont("楷体", 13));
                edit->setReadOnly(true);
                // edit->setAlignment(Qt::AlignRight);  // 不起作用

                QTextEdit* edit1 = new QTextEdit(dia);
                edit1->resize(600, 145);
                edit1->move(0, 250);
                edit1->setFont(QFont("楷体", 13));

                QPushButton* sendBtn = new QPushButton("发送", dia);
                sendBtn->resize(150, 35);
                sendBtn->move(420, 335);
                connect(sendBtn, &QPushButton::clicked, [edit, edit1](){
                    if(edit1->toPlainText().isEmpty())
                    {
                        return;
                    }
                    edit->append("我：" + edit1->toPlainText());
                    edit1->clear();
                });
                dia->show();
            });
            v->addWidget(btn);
        }
        tool->addItem(w, it.key());
    }
}

Widget::~Widget()
{
    delete ui;
}
